package com.jiebao.scan;

import android.content.Context;
import android.os.Handler;
import android.os.Message;


/**
 * 扫描包装类
 */

public class ScanHelper {

    public static ScanHelper instance = null;
    public BarcodeManager barcodeManager;
    private final int HANDLER_SCAN_TIMEOUT = 2000;
    private final int Handler_SHOW_RESULT = 2001;
    private boolean isInit = false;
    private ScanListener currentListener;

    private Handler mHandler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Handler_SHOW_RESULT:

                    if (null != msg.obj) {
                        final String barcode = msg.obj.toString().trim();
                        parseBarcode(barcode);
                    }
                    break;
                case HANDLER_SCAN_TIMEOUT: {
                    handlerTimeout((long)msg.obj);
                    break;
                }
            }
        }
    };

    public synchronized void parseBarcode(final String barcode) {
        currentListener.fillCode(barcode);
    }

    public synchronized void handlerTimeout(long timeout) {
        currentListener.timeout(timeout);
    }

    private ScanHelper() {
    }

    public synchronized static ScanHelper getInstance() {
        if (instance == null) {
            instance = new ScanHelper();
        }
        return instance;
    }

    private void loadHardScanModule(Context activity) {
        barcodeManager = BarcodeManager.getInstance();
        barcodeManager.Barcode_Open(activity, dataReceived);
    }

    public void Open_Barcode(Context activity) {

        try {
            if (!isInit) {
                loadHardScanModule(activity);
                isInit = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void closeHardScanModule() {
        if (null != barcodeManager) {
            barcodeManager.Barcode_Close();
            barcodeManager.Barcode_Stop();
        }
    }

    public void Close_Barcode() {
        try {
            currentListener = null;
            closeHardScanModule();
            isInit = false;
        } catch (Exception ignored) {
        }
    }

    public void setScanListener(ScanListener
            scanListener) {
        this.currentListener = scanListener;
    }

    private BarcodeManager.Callback dataReceived = new BarcodeManager
            .Callback() {
        @Override
        public void Barcode_Read(byte[] buffer, String codeId, int errorCode) {
            synchronized (ScanHelper.this) {
                if (null != buffer) {
                    Message msg = new Message();
                    msg.what = Handler_SHOW_RESULT;
                    msg.obj = new String(buffer);
                    mHandler.sendMessage(msg);
                    BarcodeManager.getInstance().Barcode_Stop();
                }
            }
        }

        @Override
        public void Barcode_timeout(long l) {
            synchronized (ScanHelper.this) {
                Message msg = new Message();
                msg.what = HANDLER_SCAN_TIMEOUT;
                msg.obj = l;
                mHandler.sendMessage(msg);
                BarcodeManager.getInstance().Barcode_Stop();
            }
        }
    };

}
